package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.pojo.entity.Following;
import com.example.pojo.entity.FriendShip;
import com.example.pojo.entity.UserProfile;
import com.example.pojo.vo.FriendVo;
import com.example.pojo.vo.UserVo;
import com.example.result.Result;
import com.example.service.IFriendshipService;
import com.example.service.IUserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjw
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/friendship")
public class FriendshipController {
    @Autowired
    private IFriendshipService friendshipService;
    @Autowired
    private IUserProfileService userProfileService;

    //获取好友列表
    @GetMapping("/id")
    public Result<List<FriendVo>> handleGetFriends(@RequestParam("id") Integer id) {

            System.out.println(id);
            LambdaQueryWrapper<FriendShip> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(FriendShip::getUId, id);
            List<FriendShip> list = friendshipService.list(lambdaQueryWrapper);
            List<FriendVo> friendVos = new ArrayList<>();
            List<Integer> ids = new ArrayList<>();


            for (FriendShip friendship : list) {
                ids.add(friendship.getFriendId());
            }
            List<UserProfile> userProfiles=userProfileService.listByIds(ids);
            for (FriendShip friendship : list) {
                Integer friendId = friendship.getFriendId();
                Integer friendLevel = friendship.getFriendLevel();

                // 在 userProfiles 中查找 friendId 对应的 UserProfile
                UserProfile matchingProfile =new UserProfile();
                for (UserProfile userProfile : userProfiles) {
                    if (userProfile.getId().equals(friendId)) {
                        matchingProfile = userProfile;
                        break;
                    }
                }
                    FriendVo friendVo = new FriendVo();
                    friendVo.setFriendLevel(friendLevel);
                    friendVo.setName(matchingProfile.getName());
                    friendVo.setDescription(matchingProfile.getDescription());
                    friendVo.setAvatar(matchingProfile.getAvatar());
                    friendVo.setIndexBg(matchingProfile.getIndexBg());

                    // 设置其他需要的属性...

                    friendVos.add(friendVo);

            }
            return Result.success(friendVos);

    }

}
